package cn.b.sky.mes.andon.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.andon.model.AndonAccount;
import cn.b.sky.mes.andon.model.AndonType;
import cn.b.sky.mes.andon.model.condition.AndonAccountCondition;
import cn.b.sky.mes.andon.service.AndonAccountService;
import cn.b.sky.mes.andon.service.AndonTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping("/andonAccount/*")
public class AndonAccountController extends SecurityController {

	@Resource
	AndonAccountService andonAccountService;

	@Resource
	AndonTypeService andonTypeService;

	@RequestMapping("andonAccount_list")
	public String andonAccountList(
			@ModelAttribute("condition") AndonAccountCondition condition,
			Model model, String navTabId) {

		condition.setSiteCode(this.getSiteCode());
		if (StringUtils.isEmpty(condition.getOrderField())) {
			condition.setOrderField("CreateDateTime");
		}
		List<AndonType> andonTypes = andonTypeService.getAllAndonTypes(this
				.getSiteCode());
		model.addAttribute("andonTypes", andonTypes);

		List<AndonAccount> list = andonAccountService
				.selectAndonAccountListByCondition(condition);
		int count = andonAccountService
				.selectAndonAccountCountByCondition(condition);

		condition.setData(list);
		condition.setTotalCount(count);
		model.addAttribute("page", condition);
		model.addAttribute(NAVTABID, navTabId);
		return "andon/andonAccount/andonAccount_list";
	}

	@RequestMapping("viewAndonAccount")
	public String viewAndonAccount(String navTabId, Model model,
			String andonCode) {
		AndonAccount andonAccount = andonAccountService.getAndonAccount(
				this.getSiteCode(), andonCode);
		List<AndonType> andonTypes = andonTypeService.getAllAndonTypes(this
				.getSiteCode());
		model.addAttribute("andonTypes", andonTypes);
		model.addAttribute("andonAccount", andonAccount);
		model.addAttribute(NAVTABID, navTabId);
		return "andon/andonAccount/andonAccount_view";
	}

	@RequestMapping("addAndonAccount")
	public String addAndonAccount(String navTabId, Model model) {
		List<AndonType> andonTypes = andonTypeService.getEnableAndonTypes(this
				.getSiteCode());				
		model.addAttribute("andonTypes", andonTypes);
		model.addAttribute(NAVTABID, navTabId);
		return "andon/andonAccount/andonAccount_add";
	}

	@RequestMapping("copyAndonAccount")
	public String copyAndonAccount(String navTabId, Model model,String andonCode) {
		AndonAccount andonAccount = andonAccountService.getAndonAccount(
				this.getSiteCode(), andonCode);			
		model.addAttribute("andonAccount", andonAccount);
		model.addAttribute(NAVTABID, navTabId);
		return "andon/andonAccount/andonAccount_copy";
	}
	
	 @RequestMapping("modifyAndonAccount")
	  public String modifyAndonType(String navTabId, Model model, String code) {
	    AndonAccount andonAccount = andonAccountService.getAndonAccount(this.getSiteCode(), code);
		List<AndonType> andonTypes = andonTypeService.getEnableAndonTypes(this
				.getSiteCode());				
		model.addAttribute("andonTypes", andonTypes);
	    model.addAttribute("andonAccount", andonAccount);	  
	    model.addAttribute(NAVTABID, navTabId);
	    return "andon/andonAccount/andonAccount_modify";
	  }
	 	 
	  @RequestMapping("validateAndonAccount")
	  @ResponseBody
	  public boolean validateAndonAccount(String andonCode) {
	    if (StringUtils.isEmpty(andonCode)) {
	      return true;
	    } else {
	      AndonAccount andonAccount = andonAccountService.getAndonAccount(this.getSiteCode(), andonCode.toUpperCase());
	      return andonAccount == null;
	    }
	  }
	  
	@RequestMapping("saveAndonAccount")
	@ResponseBody
	public DWZResult saveAndonAccount(String id, AndonAccount andonAccount,
			String navTabId, Model model) {
		if (null == andonAccount) {
			return ajaxError(getImeViewMessageI18n(null,
					SystemConstant.PARAMETERNULL));
		}
		andonAccount.setSiteCode(this.getSiteCode());
		if (StringUtils.isEmpty(id)) {
			setCreateInfo(andonAccount);
			AndonAccount validateAndonAccount = andonAccountService.getAndonAccount(this.getSiteCode(), andonAccount.getAndonCode());
			if (validateAndonAccount!=null) {
				return ajaxError(getImeViewMessageI18n(null,"andonCodeExist.M"));
			}
			andonAccountService.insertAndonAccount(andonAccount);
		} else {
			setModifyInfo(andonAccount);
			andonAccountService.updateAndonAccount(andonAccount);
		}
		return dialogAjaxDone(
				getImeViewMessageI18n(null, "successfulOperation.M"), navTabId,
				null, null, null);
	}

	@RequestMapping("deleteAndonAccount")
	@ResponseBody
	public DWZResult deleteAndonAccount(String navTabId, Model model,
			String codes, HttpServletRequest request) {
		String[] idArr = codes.split(",");
		List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
		int failures = 0;
		for (int i = 0; i < idArr.length; i++) {
			String code = idArr[i];
			String message = null;
			message = andonAccountService.deleteAndonAccount(
					this.getSiteCode(), code);
			boolean flag = true;
			if (!StringUtils.isEmpty(message)) {
				failures += 1;
				flag = false;
				message = getImeViewMessageI18n(
						SecurityContextUtil.getLanguageCode(), message);
			}
			BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
			reports.add(report);
		}
		if (failures == 0) {
			return navTabAjaxDone(
					navTabId,
					null,
					null,
					null,
					getImeViewMessageI18n(this.getCurrentLanguage(),
							"successfulOperation.M"), null);
		} else {
			return batchjobAjaxDone(request, reports);
		}
	}
	@RequestMapping("disableAndonAccount")
	  @ResponseBody
	  public DWZResult disableAndonAccount(String navTabId, Model model, String codes, HttpServletRequest request) {
	    String[] idArr = codes.split(",");
	    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
	    int failures = 0;
	    for (int i = 0; i < idArr.length; i++) {
	      String code = idArr[i];
	      String message = null;
	      message = andonAccountService.disableAndonAccount(this.getSiteCode(), code);
	      boolean flag = true;
	      if (!StringUtils.isEmpty(message)) {
	        failures += 1;
	        flag = false;
	        message = getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), message);
	      }
	      BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
	      reports.add(report);
	    }
	    if (failures == 0) {
	      return navTabAjaxDone(navTabId, null, null, null,
	          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
	    } else {
	      return batchjobAjaxDone(request, reports);
	    }
	  }
	
	 @RequestMapping("enableAndonAccount")
	  @ResponseBody
	  public DWZResult enableAndonAccount(String navTabId, Model model, String codes, HttpServletRequest request) {
	    String[] idArr = codes.split(",");
	    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
	    int failures = 0;
	    for (int i = 0; i < idArr.length; i++) {
	      String code = idArr[i];
	      String message = null;
	      message = andonAccountService.enableAndonAccount(this.getSiteCode(), code);
	      boolean flag = true;
	      if (!StringUtils.isEmpty(message)) {
	        failures += 1;
	        flag = false;
	        message = getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), message);
	      }
	      BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
	      reports.add(report);
	    }
	    if (failures == 0) {
	      return navTabAjaxDone(navTabId, null, null, null,
	          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
	    } else {
	      return batchjobAjaxDone(request, reports);
	    }
	  }
}
